package messageAPI.Tests;

import messageAPI.PerformanceInstrumentationServiceFactory;
import messageAPI.Core.IPerformanceInstrumentationService;
import messageAPI.Core.IPerformanceInstrumentationServiceFactory;
import messageAPI.Core.PerformanceEvent;

import com.rabbitmq.client.QueueingConsumer;
import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.List;
public class InstrumentationHandlerTests extends TestCase {

    public final void testCreateEntry() throws Throwable
	{
        System.out.println(" [MessageAPITest] *------------------START TEST: testCreateEntry-------------------*");
        int numberOfMessageInTest=1;

        //SETUP TEST
        boolean testRunning = true;
        TestUtilities.setupTests();
        System.out.println(" [MessageAPITest] Consumer - Waiting for messages.");

        System.out.println(" [MessageAPITest] Test - Create Publisher.");

        //Start measuring submit time
        TestUtilities.startTimer();
	    
	    IPerformanceInstrumentationServiceFactory performanceInstrumentationServiceFactory = new PerformanceInstrumentationServiceFactory();
	    IPerformanceInstrumentationService performanceInstrumentationService = performanceInstrumentationServiceFactory.getPerformanceInstrumentationService();
		
		long timeStamp=1010101;
		long value = 101;
		
		PerformanceEvent performanceEvent = new PerformanceEvent("error",timeStamp,value);
		
		System.out.println(" [MessageAPITest] Waiting for messages.");
		
		
		System.out.println(" [MessageAPITest] Sending Test Message");

        List<PerformanceEvent> eventList = new ArrayList<PerformanceEvent>();
        eventList.add(performanceEvent);
        performanceInstrumentationService.sendEvents(eventList);

        //Stop measuring submit time
        TestUtilities.stopTimer();

        //RECEIVE TEST DATA BACK

        int numberOfMessagesReceived=0;

        while (testRunning) {
            QueueingConsumer.Delivery delivery = TestUtilities.testConsumer.nextDelivery();
            String message = new String(delivery.getBody());


            numberOfMessagesReceived++;

            if(numberOfMessagesReceived>=numberOfMessageInTest)
            {
                testRunning=false;
            }
        }
        System.out.println(" [MessageAPITest] Consumer Received " + numberOfMessagesReceived + " Messages");

        System.out.println(" [MessageAPITest] Test completed.");

        System.out.println(" [MessageAPITest] Shutting Down Test.");
        TestUtilities.shutDownTests();
	   }
	public final void testCreateEntryWithContextualData() throws Throwable
	{
        int numberOfMessageInTest=1;
        System.out.println(" [MessageAPITest] *------------------START TEST: testCreateEntryWithContextualData-------------------*");
        //SETUP TEST
        boolean testRunning = true;
        TestUtilities.setupTests();
        System.out.println(" [MessageAPITest] Consumer - Waiting for messages.");

        System.out.println(" [MessageAPITest] Test - Create Publisher.");

        //Start measuring submit time
        TestUtilities.startTimer();
	    
	    IPerformanceInstrumentationServiceFactory performanceInstrumentationServiceFactory = new PerformanceInstrumentationServiceFactory();
	    IPerformanceInstrumentationService performanceInstrumentationService = performanceInstrumentationServiceFactory.getPerformanceInstrumentationService();
		
		long timeStamp=1010101;
		long value = 101;
		
		PerformanceEvent performanceEvent = new PerformanceEvent("error",timeStamp,value);
		
		performanceEvent.addContextValue("someContextId", "someContextValue");
		
		
		System.out.println(" [MessageAPITest] Waiting for messages.");
		
		
		System.out.println(" [MessageAPITest] Sending Test Message");

        List<PerformanceEvent> eventList = new ArrayList<PerformanceEvent>();
        eventList.add(performanceEvent);
        performanceInstrumentationService.sendEvents(eventList);

        //Stop measuring submit time
        TestUtilities.stopTimer();

        //RECEIVE TEST DATA BACK

        int numberOfMessagesReceived=0;

        while (testRunning) {
            QueueingConsumer.Delivery delivery = TestUtilities.testConsumer.nextDelivery();
            String message = new String(delivery.getBody());


            numberOfMessagesReceived++;

            if(numberOfMessagesReceived>=numberOfMessageInTest)
            {
                testRunning=false;
            }
        }
        System.out.println(" [MessageAPITest] Consumer Received " + numberOfMessagesReceived + " Messages");

        System.out.println(" [MessageAPITest] Test completed.");

        System.out.println(" [MessageAPITest] Shutting Down Test.");
        TestUtilities.shutDownTests();
	}
	public final void testCreateMultipleSequentialEntries() throws Throwable
	{
        System.out.println(" [MessageAPITest] *------------------START TEST: testCreateMultipleSequencialEntries-------------------*");

        int numberOfMessageInTest=50000;

        //SETUP TEST
        boolean testRunning = true;
        TestUtilities.setupTests();
        System.out.println(" [MessageAPITest] Consumer - Waiting for messages.");

        System.out.println(" [MessageAPITest] Test - Create Publisher.");

        //Start measuring submit time
        TestUtilities.startTimer();
	    
	    IPerformanceInstrumentationServiceFactory performanceInstrumentationServiceFactory = new PerformanceInstrumentationServiceFactory();
	    IPerformanceInstrumentationService performanceInstrumentationService = performanceInstrumentationServiceFactory.getPerformanceInstrumentationService();
		
		long timeStamp=1010101;
		long value = 101;
		

		System.out.println(" [MessageAPITest] Waiting for messages.");

		System.out.println(" [MessageAPITest] Sending Test Messages");

        List<PerformanceEvent> eventList = new ArrayList<PerformanceEvent>();

        for (int i=0;i<numberOfMessageInTest;i++)
        {
            PerformanceEvent performanceEvent = new PerformanceEvent("error",timeStamp,value);

            eventList.add(performanceEvent);
        }

        performanceInstrumentationService.sendEvents(eventList);

        //Stop measuring submit time
        TestUtilities.stopTimer();

        //RECEIVE TEST DATA BACK

        int numberOfMessagesReceived=0;

        while (testRunning) {
            QueueingConsumer.Delivery delivery = TestUtilities.testConsumer.nextDelivery();
            String message = new String(delivery.getBody());


            numberOfMessagesReceived++;

            if(numberOfMessagesReceived>=numberOfMessageInTest)
            {
                testRunning=false;
            }
        }
        System.out.println(" [MessageAPITest] Consumer Received " + numberOfMessagesReceived + " Messages");

        System.out.println(" [MessageAPITest] Test completed.");

        System.out.println(" [MessageAPITest] Shutting Down Test.");
        TestUtilities.shutDownTests();
	   }
	public final void testCreateMultipleSequentialEntriesWithContextualData() throws Throwable
	{
        System.out.println(" [MessageAPITest] *------------------START TEST: testCreateMultipleSequencialEntriesWithContextualData-------------------*");
		int numberOfMessageInTest=100000;

        boolean testRunning=true;
		//SETUP TEST

        TestUtilities.setupTests();

        System.out.println(" [MessageAPITest] Consumer - Waiting for messages.");

        System.out.println(" [MessageAPITest] Test - Create Publisher.");

        //Start measuring submit time
        TestUtilities.startTimer();
	    
	    IPerformanceInstrumentationServiceFactory performanceInstrumentationServiceFactory = new PerformanceInstrumentationServiceFactory();
	    IPerformanceInstrumentationService performanceInstrumentationService = performanceInstrumentationServiceFactory.getPerformanceInstrumentationService();
		
		long timeStamp=1010101;
		long value = 101;

		System.out.println(" [MessageAPITest] Publisher - Sending " +numberOfMessageInTest+ " Test Messages");


        List<PerformanceEvent> eventList = new ArrayList<PerformanceEvent>();

		for (int i=0;i<numberOfMessageInTest;i++)
		{
			PerformanceEvent performanceEvent = new PerformanceEvent("error",timeStamp,value);
			performanceEvent.addContextValue("someContextId", "someContextValue");

            eventList.add(performanceEvent);
		}

        performanceInstrumentationService.sendEvents(eventList);

		//Stop measuring submit time
        TestUtilities.stopTimer();

        //RECEIVE TEST DATA BACK

        int numberOfMessagesReceived=0;

        while (testRunning) {
            QueueingConsumer.Delivery delivery = TestUtilities.testConsumer.nextDelivery();
            String message = new String(delivery.getBody());


            numberOfMessagesReceived++;

            if(numberOfMessagesReceived>=numberOfMessageInTest)
            {
            testRunning=false;
            }
        }
        System.out.println(" [MessageAPITest] Consumer Received " + numberOfMessagesReceived + " Messages");

        System.out.println(" [MessageAPITest] Test completed.");

        System.out.println(" [MessageAPITest] Shutting Down Test.");
        TestUtilities.shutDownTests();


    }
}
